package com.bestvike.stone.spring.rest.common;

import com.alibaba.fastjson.annotation.JSONField;
import com.bestvike.stone.spring.exception.ErrorCode;
import com.bestvike.stone.spring.rest.IResponse;
import com.bestvike.stone.spring.trace.rest.ErrorHandler;
import lombok.Data;

/**
 * Created by 许崇雷 on 2017-10-08.
 */
@Data
public final class CommonResponse<TBody> implements IResponse<TBody> {
    @JSONField(ordinal = 1)
    private String code;

    @JSONField(ordinal = 2)
    private String msg;

    @JSONField(ordinal = 3)
    private TBody body;

    CommonResponse() {
    }

    //业务中请勿使用该方法
    public static <TBody> CommonResponse<TBody> create(String code, String msg) {
        CommonResponse<TBody> response = new CommonResponse<>();
        response.setCode(code);
        response.setMsg(msg);
        return response;
    }

    public static <TBody> CommonResponse<TBody> success() {
        return create(ErrorCode.SUCCESS.getCode(), ErrorCode.SUCCESS.getMsg());
    }

    public static <TBody> CommonResponse<TBody> success(TBody body) {
        CommonResponse<TBody> response = create(ErrorCode.SUCCESS.getCode(), ErrorCode.SUCCESS.getMsg());
        response.setBody(body);
        return response;
    }

    public void setCode(String code) {
        this.code = ErrorHandler.getCode(code);
    }
}
